package com.shootingGame
{
	import com.common.IGamePlay;
	import com.common.screens.PlayScreen;
	import com.shootingGame.mapClasses.Map;
	import com.shootingGame.mapClasses.Maps;
	import com.shootingGame.renderableClasses.GameObject;
	import com.shootingGame.weaponsClasses.ordinance.Ordinance;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	public class GamePlayManager implements IGamePlay
	{
		public static var TILE_WIDTH : Number;
		public static var TILE_HEIGHT : Number;
		public static var MAP_NUMTILES_HORIZONTAL : Number;
		public static var MAP_NUMTILES_VERTICAL : Number;
		public var player1 : Player;		
		public var playScreen : PlayScreen;
		protected var updateTimer : Timer;
		protected static const UPDATE_DELAY : Number = 1;
		protected var time_Elapsed:Number;
		protected var time_LastUpdate : Number;
		public var ordinanceHolder:MovieClip;
		public var playerHolder:MovieClip;
		public var generalHolder:MovieClip;
		public var pickupHolder:MovieClip;
		public var effectsHolder:MovieClip;
		public var mapHolder : MovieClip;
		public var activeMap:Map;
		public var maps:Maps;
		public var clickDetector : MovieClip;
		public var renderablesList : Array;
		public var ordinanceList : Array;
		public var playerList : Array;
		public var pickupList : Array;
		public var generalList : Array;
		public var viewRect:Rectangle;
		public var ordinance_CachedList : Array;
		public var players_CachedList : Array;
		public var weapons_CachedList : Array;
		public var pickups_CachedList : Array;
		protected var desiredViewPosition : Point;
		
//		public var darknessHolder : MovieClip;
		//public var ordinanceCollisionList:CollisionList;
		
		static private var _instance : GamePlayManager;
		static public function get instance() : GamePlayManager{
			return _instance;
		}
		public static var stageWidth : Number;
		public static var stageHeight : Number;
		
		
		
		public function GamePlayManager(playScreen:PlayScreen, initializeImmediately:Boolean = true)
		{
			this.playScreen = playScreen;
			_instance = this;
			//UPDATE_DELAY = 1000/stage.frameRate;
			clickDetector = playScreen.clickDetector;
			ordinanceHolder = playScreen.arena.ordinanceHolder;
			playerHolder = playScreen.arena.playerHolder;
			generalHolder = playScreen.arena.generalHolder;
			pickupHolder = playScreen.arena.pickupHolder;
			effectsHolder = playScreen.arena.effectsHolder;
			mapHolder = playScreen.arena.mapHolder;
//			darknessHolder = playScreen.arena.darknessHolder;
			stageWidth = playScreen.width;
			stageHeight = playScreen.height;
			renderablesList = new Array();
			ordinanceList = new Array();
			playerList = new Array();
			pickupList = new Array();
			generalList = new Array();
			loadMap("Sample Map");
			clickDetector.addEventListener(MouseEvent.MOUSE_UP, onMouse_Up);
			//playScreen.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouse_Move);
			clickDetector.addEventListener(MouseEvent.MOUSE_DOWN, onMouse_Down);
			playScreen.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
			playScreen.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyReleased);
			playScreen.stage.addEventListener(Event.ENTER_FRAME, onRender);
			updateTimer = new Timer(UPDATE_DELAY);
			updateTimer.addEventListener(TimerEvent.TIMER, onUpdate);
			if(initializeImmediately)
				initializeGame();
		}

		virtual public function onUpdate(evt : TimerEvent) : void {
			var time:Number = flash.utils.getTimer();
			time_Elapsed = time  - time_LastUpdate;
			time_LastUpdate = time;
//			for(var i:int = 0; i<renderablesList.length; i++){
//				renderablesList[i].update(time_Elapsed);
//			}
			var ordinance : Ordinance;
			var player : Player;
			var i : int;
			var j : int;
			var n : int;
			
			var tm:int = flash.utils.getTimer();
			
			for(i = 0; i < time_Elapsed; i++) {
				n = ordinanceList.length
				for(j = 0; j < ordinanceList.length; j++) {
					if(!ordinanceList[j].isExpired){
						ordinance = ordinanceList[j];
						ordinance.update();
					}
				}
				n = playerList.length;
				for(j = 0; j < n; j++) {
					if(!playerList[j].isExpired)
						playerList[j].update();
				}
				n = pickupList.length;
				for(j = 0; j < n; j++) {
					if(!pickupList[j].isExpired)
						pickupList[j].update();
				}
				n = generalList.length;
				for(j = 0; j < n; j++) {
					if(!generalList[j].isExpired)
						generalList[j].update();
				}
			}
			detectCollisions();
		}
		
		virtual public function onRender(evt : Event) : void {
			var i :int;
			var n:int = renderablesList.length;
			for(i = 0; i<n; i++){
				renderablesList[i].render();
			}			
		}
		
		virtual protected function detectCollisions() : void {
			ordinanceCollisions();
			playerCollisions();
		}
		
		virtual protected function ordinanceCollisions() : void
		{
		}
		
		virtual protected function playerCollisions() : void
		{
		}
		
		public function get mouseX() : Number{
			return clickDetector.mouseX;
		}
		
		public function get mouseY() : Number{
			return clickDetector.mouseY
		}

		virtual protected function onMouse_Down(event : MouseEvent) : void {
			if(player1){
				if(!player1.keyStates.MOUSELEFT_DOWN)
					player1.keyStates.MOUSE_JUST_TOGGLED = true;
				player1.keyStates.MOUSELEFT_DOWN = true;
			}
		}

		virtual protected function onMouse_Up(event : MouseEvent) : void {
			if(player1){
				if(player1.keyStates.MOUSELEFT_DOWN)
					player1.keyStates.MOUSE_JUST_TOGGLED = true;
				player1.keyStates.MOUSELEFT_DOWN = false;
			}
		}
		
		virtual protected function onKeyPress(evt : KeyboardEvent) : void {
			if(player1){
				if(String.fromCharCode(evt.keyCode) == "W")
					player1.keyStates.UP = true;
				
				if(String.fromCharCode(evt.keyCode) == "S")
					player1.keyStates.DOWN = true;
				
				if(String.fromCharCode(evt.keyCode) == "A")
					player1.keyStates.LEFT = true;
				
				if(String.fromCharCode(evt.keyCode) == "D")
					player1.keyStates.RIGHT = true;
			}
		}
		
		virtual protected function onKeyReleased(evt : KeyboardEvent) : void {
			if(player1){
				if(String.fromCharCode(evt.keyCode) == "W")
					player1.keyStates.UP = false;
				
				if(String.fromCharCode(evt.keyCode) == "S")
					player1.keyStates.DOWN = false;
				
				if(String.fromCharCode(evt.keyCode) == "A")
					player1.keyStates.LEFT = false;
				
				if(String.fromCharCode(evt.keyCode) == "D")
					player1.keyStates.RIGHT = false;
			}
		}
		
		virtual protected function loadMap(title:String) : void {
		}
		
		virtual protected function initializeGame() : void {
		}
		
		virtual protected function kill(p:Player) : void {
			
		}
		
	}
}